import ACCESS_ENUM from "@/access/accessEnum";
import { StoreOptions } from "vuex";

export default {
  namespaced: true,
  state: () => ({
    user: {
      permission: "0",
      username: "未登录",
      name: "",
      password: "",
      signature: "",
      email: "",
      avatar: "",
      status: "noLogin",
    },
  }),
  actions: {
    initializeUser({ commit }) {
      commit('initializeUser');
    },
    getLoginUser({ commit, state }, payload) {
      commit("updateUser", payload); // 修正这里的 mutation 名称
    }
  },
  mutations: {
    updateUser(state, payload) {
      state.user = payload;
      state.user.status = payload?.status||"Login";
      localStorage.setItem('user',JSON.stringify(payload));
    },
    initializeUser(state) {   // 从本地获取登入信息
      const userString = localStorage.getItem('user');
      if (userString) {
        const userData = JSON.parse(userString);
        state.user = { ...state.user, ...userData };
      }
    }
  },
  // 初始化时调用 initializeUser mutation
  // plugins: [(store) => {
  //   store.subscribe((mutation, state) => {
  //     if (mutation.type === 'initializeUser') {
  //       store.commit('initializeUser');
  //     }
  //   });
  // }],
} as StoreOptions<any>;
